﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WiseLink.RDS.Resource;
using System.ComponentModel;

namespace CCF20Model
{
    partial class Device : IDataErrorInfo
    {
        public Device()
        {
         
        }

        partial void OnDeviceKeyChanged()
        {
            this.RemoveError("DeviceKey");
            //this.AddError("DeviceKey", "姓名不可为空");
            
        }
        partial void OnDeviceStatusChanged()
        {
            if (this.DeviceStatus == WiseLink.RDS.Resource.DeviceStatus.InStock)
            {

                //Because we want to keep the vehicle status when we change device (device may be broken)
                #region clean up the vechile related property
                if (this.Vehicle != null)
                {
                    if (this.Vehicle.VehicleMaintenanceRecords != null)
                    {
                        CCF20Entities ccf20Ent = new CCF20Entities();
                        IList<VehicleMaintenanceRecord> vmrList = ccf20Ent.VehicleMaintenanceRecords.Where(m => m.VehicleID == this.VehicleID).ToList();
                        foreach (VehicleMaintenanceRecord vmr in vmrList)
                        {
                            ccf20Ent.VehicleMaintenanceRecords.DeleteObject(vmr);
                        }
                        ccf20Ent.SaveChanges();
                    }
                    if (this.Vehicle.Faults != null)
                    {
                        foreach (Fault f in this.Vehicle.Faults.Where(f => f.IsActive))
                        {
                            f.IsActive = false;
                            f.CloseDate = DateTime.Now;
                            f.CloseReason = "归库";
                        }
                    }
                    if (this.Vehicle.VehicleMaintenanceHistories != null)
                    {
                        foreach (VehicleMaintenanceHistory vmh in this.Vehicle.VehicleMaintenanceHistories)
                        {
                            vmh.IsActive = false;
                            vmh.CloseDate = DateTime.Now;
                            vmh.CloseReason = "归库";
                        }
                    }
                    if (this.Vehicle.SMSHistories != null)
                    {
                        foreach (SMSHistory sms in this.Vehicle.SMSHistories)
                        {
                            sms.IsActive = false;
                            sms.CloseDate = DateTime.Now;
                            sms.CloseReason = "归库";
                        }
                    }
                    if (this.Vehicle.ContactHistories != null)
                    {
                        foreach (ContactHistory ch in this.Vehicle.ContactHistories)
                        {
                            ch.IsActive = false;
                            ch.CloseDate = DateTime.Now;
                            ch.CloseReason = "归库";
                        }
                    }
                }
                #endregion

            }

        }

        #region IDataErrorInfo Members

        private Dictionary<string, string> m_validationErrors = new Dictionary<string, string>();

        private void AddError(string columnName, string msg)
        {
            if (!m_validationErrors.ContainsKey(columnName))
            {
                m_validationErrors.Add(columnName, msg);
            }

        }

        private void RemoveError(string columnName)
        {
            if (m_validationErrors.ContainsKey(columnName))
            {
                m_validationErrors.Remove(columnName);
            }
        }

        public string Error
        {
            get
            {
                if (m_validationErrors.Count > 0)
                {
                    return "Customer data is invalid";
                }
                else return null;
            }
        }

        public string this[string columnName]
        {
            get
            {
                if (m_validationErrors.ContainsKey(columnName))
                {
                    return m_validationErrors[columnName];
                }
                else return null;
            }
        }

        #endregion
    }
}
